package com.example.personalmemo.slice;

import com.example.personalmemo.ResourceTable;
import com.example.personalmemo.Util.GetTime;
import com.example.personalmemo.dataName.DataAbilityName;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.rdb.ValuesBucket;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

public class RecordPageSlice extends AbilitySlice {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(0, 0, "Demo");

    private final String FOR_MAT = "yyyy/MM/dd hh:mm:ss";

    private TextField mTitleText;
    private TextField mTextFeild;

    private DataAbilityHelper dataAbilityHelper;

    @Override
    public void onStart(Intent intent) {

        HiLog.info(LABEL_LOG, "RecordPageSlice onStart ");

        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_record_page);

        dataAbilityHelper = DataAbilityHelper.creator(getContext());
        initCompent();



    }

    /**
     * 初始化组件
     */
    private void initCompent() {

        Button savaBtn = findComponentById(ResourceTable.Id_sava_btn);
        savaBtn.setClickedListener(this::savaData);

        mTitleText = findComponentById(ResourceTable.Id_title_textfeild);
        mTextFeild = findComponentById(ResourceTable.Id_good_record_textfiled);

    }

    /**
     * 保存记录好的信息
     */

    private void savaData(Component component) {
        HiLog.info(LABEL_LOG, "savaData");

        // 获取标题信息
        String titleText = mTitleText.getText();
        // 获取文本域信息
        String text = mTextFeild.getText();
        // 获取系统当前时间
        String time = GetTime.getTime(FOR_MAT);

        if (text == null) {
            new ToastDialog(getContext()).setText("未记录消息").show();
            HiLog.info(LABEL_LOG, "未记录消息");

        }
        ValuesBucket bucket = new ValuesBucket();

        bucket.putString("title", titleText);
        bucket.putString("textinfo", text);
        bucket.putString("time", time);

        try {

            int insert = dataAbilityHelper.insert(Uri.parse(DataAbilityName.DATA_URI + "/infodata/sava"), bucket);

            if (insert != -1) {
                new ToastDialog(getContext()).setText("保存成功").show();

                // 清理
                Cleans();
                present(new MainAbilitySlice(), new Intent());
            } else {
                new ToastDialog(getContext()).setText("保存失败，请联系管理员").show();
            }

        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }


    }

    // 清理上次记录
    private void Cleans() {
        HiLog.info(LABEL_LOG, "informationPageSlice Cleans");
        mTitleText.setText(null);
        mTextFeild.setText(null);

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
